//
//  CWCMineViewController.m
//  ChuanNews
//
//  Created by dllo on 16/4/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CWCMineViewController.h"
#import "BaseHeader.h"
#import "CWCNewsModel.h"
#import "CWCSaveViewController.h"
#import "AppDelegate.h"
#import "CWCAboutMineViewController.h"
#import "SDImageCache.h"
#import "JXLDayAndNightMode.h"
@interface CWCMineViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)UIImageView *imgView;
@property(nonatomic, copy)NSArray *Arr;
@property(nonatomic, retain)UISwitch *swi;
@end

@implementation CWCMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的";
    self.navigationController.navigationBar.translucent = NO;
    self.Arr = [NSArray arrayWithObjects:@"     🎁    我的收藏", @"    📃     清除缓存 ", @"    🌛     夜间模式", @"    👦    关于我们", @"    ♻️    退出APP", nil];
    self.tableView = [[UITableView alloc]init];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.frame = self.view.bounds;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle =UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableView];
//    [self.tableView registerClass:[CWCBaseTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.tableView reloadData];
    
    self.swi = [[UISwitch alloc]initWithFrame:CGRectMake(300, 396, 100, 50)];
    [self.swi addTarget:self action:@selector(switchValueChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.swi];
    [self.swi release];
    
    self.imgView = [[ UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    self.imgView.clipsToBounds = YES;
    //self.imgView.contentMode = UIViewContentModeScaleAspectFill;
    self.imgView.image = [UIImage imageNamed:@"Screenshot_2016-05-03-09-29-53_com.netease.newsre.jpg"];
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    [headView addSubview:self.imgView];
    self.tableView.tableHeaderView = headView;
    
    
}


-(void)switchValueChange:(UISwitch *)swi{
    if (swi.on) {
        [[JXLDayAndNightManager shareManager] nightMode];
    }else{
        [[JXLDayAndNightManager shareManager] dayMode];
    }
    
    
//    [self setNeedsStatusBarAppearanceUpdate];
//    swi.selected = !swi.selected;
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    NSString *password = [user objectForKey:@"night"];
//    if ([password isEqualToString:@"yes"]) {
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"CWC" object:@"no" ];
//        swi.selected = YES;
//    }else{
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"CWC" object:@"yes"];
//        swi.selected = NO;
//    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.Arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *result = @"result";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:result];
    if (!cell) {
        cell = [[CWCBaseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:result];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:20];
    cell.backgroundColor = [UIColor whiteColor];
    cell.textLabel.text = [self.Arr objectAtIndex:indexPath.row];
    return  cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CWCSaveViewController *vc = [[CWCSaveViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 1){
        float  tempSize;
        NSString *path = [NSSearchPathForDirectoriesInDomains(13, 1, YES)lastObject];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:path]) {
            NSArray  *childerFiles = [fileManager subpathsAtPath:path];
            for (NSString *fileName in childerFiles) {
                NSString *absolutePath = [path stringByAppendingPathComponent:fileName ];
                tempSize += [self fileSizeAtPath:absolutePath];
            }
            tempSize += [[SDImageCache sharedImageCache]getSize] / 1024 / 1024;
        }
        NSLog(@"%f",tempSize);
        NSString *message = [NSString stringWithFormat:@"清理缓存(%.2fM)",tempSize];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSFileManager *manager = [NSFileManager defaultManager];
            [manager removeItemAtPath:path error:nil];
   
        }];
        
        [alert addAction:action2];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:nil];
    }else if (indexPath.row == 4){
        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        UIWindow * window = app.window;
        [UIView animateWithDuration:0.5 animations:^{
            window.alpha = 0;
            window.frame = CGRectMake(0, WIDTH, 0, 0);
        } completion:^(BOOL finished) {
            
            exit(0);
        }];
    }else if(indexPath.row == 3){
        CWCAboutMineViewController *aboutMine = [[CWCAboutMineViewController alloc]init];
        aboutMine.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:aboutMine animated:YES completion:^{
            
        }];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}
-(float)fileSizeAtPath:(NSString *)path{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return  size / 1024 / 1024;
    }
    return 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
